package com.yh.autobio.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jjobes.slidedatetimepicker.SlideDateTimeListener;
import com.github.jjobes.slidedatetimepicker.SlideDateTimePicker;
import com.google.gson.Gson;
import com.gxz.PagerSlidingTabStrip;
import com.yh.autobio.AppConfig;
import com.yh.autobio.AppContext;
import com.yh.autobio.R;
import com.yh.autobio.api.ErrorHandler;
import com.yh.autobio.api.RetrofitSingleton;
import com.yh.autobio.base.BaseFragment;
import com.yh.autobio.base.FragmentsViewPagerAdapter;
import com.yh.autobio.base.StickyNavLayout;
import com.yh.autobio.common.ArithUtil;
import com.yh.autobio.common.FullyLinearLayoutManager;
import com.yh.autobio.common.PLog;
import com.yh.autobio.common.StringUtils;
import com.yh.autobio.common.TDevice;
import com.yh.autobio.common.Time;
import com.yh.autobio.common.UIHelper;
import com.yh.autobio.gddb.gbmanager.MatCountManager;
import com.yh.autobio.gddb.gbmanager.MatHeadManager2;
import com.yh.autobio.gddb.gbmanager.ScoreEntryManager;
import com.yh.autobio.gddb.gdbean.MatBeanCount;
import com.yh.autobio.gddb.gdbean.MatHead;
import com.yh.autobio.modules.bean.CM;
import com.yh.autobio.modules.bean.CustPhone;
import com.yh.autobio.modules.bean.MatSubBean;
import com.yh.autobio.modules.bean.Result;
import com.yh.autobio.modules.bean.SimpleBackPage;
import com.yh.autobio.modules.listener.OnFlingChangedListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：ADDSaleOutFragment
 * <p>
 * 创建时间：2017.06.04.16:47
 * *****************************
 */
public class ADDSaleOutFragment extends BaseFragment {


    @InjectView(R.id.tv_date)
    TextView tvDate;
    @InjectView(R.id.ll_date)
    LinearLayout llDate;
    @InjectView(R.id.tv_gh_unit)
    TextView tvGhUnit;
    @InjectView(R.id.ll_gh_unit)
    LinearLayout llGhUnit;
    @InjectView(R.id.tv_sales_way)
    TextView tvSalesWay;
    @InjectView(R.id.ll_sales_way)
    LinearLayout llSalesWay;
    @InjectView(R.id.tv_paper)
    TextView tvAllMoney;//合计金额
    @InjectView(R.id.tv_bz)
    EditText tvBz;
    @InjectView(R.id.tv_pgno)
    TextView tvPgno;
    @InjectView(R.id.tv_pgno_type)
    TextView tvPgnoType;
    @InjectView(R.id.ll_pgno_type)
    LinearLayout llPgnoType;
    @InjectView(R.id.ll_pgno)
    LinearLayout llPgno;


    @InjectView(R.id.tv_sale)
    TextView tvSale;
    @InjectView(R.id.ll_sale)
    LinearLayout llSale;
    @InjectView(R.id.tv_cust_phone)
    TextView tvCustPhone;
    @InjectView(R.id.ll_cust_phone)
    LinearLayout llCustPhone;
    @InjectView(R.id.tv_fix_phone)
    EditText tvFixPhone;
    @InjectView(R.id.tv_fh_warehouse)
    TextView tvFhWarehouse;
    @InjectView(R.id.ll_fh_warehouse)
    LinearLayout llFhWarehouse;
    @InjectView(R.id.tv_mob_phone)
    EditText tvMobPhone;
    @InjectView(R.id.recyclerview)
    RecyclerView mRecycleview;
    @InjectView(R.id.img_gotop)
    ImageView imgGotop;

    private String mType;
    private MatHead head;
    private String mHeadid;
    //    private MatOrGiftJPageFragment mFragment;
    private boolean isSave;

    private FragmentsViewPagerAdapter mAdapter;

    @InjectView(R.id.id_stickynavlayout_indicato)
    PagerSlidingTabStrip pagerSlidingTabStrip;
    @InjectView(R.id.id_stickynavlayout_viewpage)
    ViewPager viewPager;
    @InjectView(R.id.id_stick)
    StickyNavLayout idStick;

    public boolean isStick;
    public static OnPageChangeListener mlistener;

    public static void setonPageChangeListener(OnPageChangeListener listeners) {
        mlistener = listeners;
    }

    public interface OnPageChangeListener {
        void onPageSelected(int position);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(mActivity, R.layout.fragment_ware, null);
        ButterKnife.inject(this, view);
        initView(view);
        initData();
        return view;
    }

    public void setAllMoney(double money) {
        tvAllMoney.setText(money + "");
    }


    @Override
    public void initView(View view) {
        super.initView(view);
        setHasOptionsMenu(true);
        TDevice.hideSoftKeyboard(mActivity);
        llGhUnit.setOnClickListener(this);
        llSalesWay.setOnClickListener(this);
        llFhWarehouse.setOnClickListener(this);
        llCustPhone.setOnClickListener(this);
        llSale.setOnClickListener(this);
        llDate.setOnClickListener(this);
        llPgno.setOnClickListener(this);
        llPgnoType.setOnClickListener(this);
        isSave = false;
        FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(getContext());
        mRecycleview.setNestedScrollingEnabled(false);
        //设置布局管理器
        mRecycleview.setLayoutManager(linearLayoutManager);

        String nowYMDHMSTime = Time.getNowYMDHMSTime();

        this.head = new MatHead();
        Bundle bundle = getArguments();
        mType = bundle.getString("TYPE");
        if ("show".equals(mType)) {
            mHeadid = bundle.getString("ID");
            long Id = Long.parseLong(mHeadid);
            head = MatHeadManager2.getInstance(mActivity).getHeadById(Id + "", "2");
            if (head != null) {
                tvDate.setText(head.getDate());
                tvSalesWay.setText(head.getXsfsName());
                tvGhUnit.setText(head.getGhdwName());
                tvPgno.setText(head.getHybName());
                tvSale.setText(head.getYwyName());
                tvCustPhone.setText(head.getKfdhName());
                tvFhWarehouse.setText(head.getFhckName());
                tvPgnoType.setText(head.getSklxName());
                tvBz.setText(head.getDesc());
            }
        } else {
            tvDate.setText(nowYMDHMSTime);
            head.setDate(nowYMDHMSTime);
            head.setFtype("2");//2代表销售出库，3代表退货
            head.setUserName(AppContext.getInstance().getCurrentUser().getName());
        }

        idStick.setOnStickStateChangeListener(new StickyNavLayout.onStickStateChangeListener() {
            @Override
            public void isStick(boolean isStick) {
                ADDSaleOutFragment.this.isStick = isStick;
                if (isStick) {
//                    imgGotop.setVisibility(View.VISIBLE);
                } else {
                    imgGotop.setVisibility(View.GONE);
                }
            }

            @Override
            public void scrollPercent(float percent) {
            }
        });

    }

    @Override
    public void initData() {
        super.initData();

        ArrayList<BaseFragment> fragments = new ArrayList<>();
        final ComListFragment comListFragment = new ComListFragment();
        final GiftListFragment giftListFragment = new GiftListFragment();

        comListFragment.setOnAllMoneyChanged(new ComListFragment.OnAllMoneyChanged() {
            @Override
            public void moneyChanged(double allmoney) {
                tvAllMoney.setText(allmoney + "");
            }
        });

        Bundle bundle = new Bundle();
        bundle.putString("HEADID", mHeadid);
        bundle.putString("TYPE", mType);
        comListFragment.setArguments(bundle);
        giftListFragment.setArguments(bundle);

//        mActivity.getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, mFragment).commit();

        fragments.add(comListFragment);
        fragments.add(giftListFragment);


        mAdapter = new FragmentsViewPagerAdapter(mActivity.getSupportFragmentManager(), fragments);
        viewPager.setAdapter(mAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (imgGotop.getVisibility()==View.VISIBLE){//0--显示，8--隐藏
                    imgGotop.setVisibility(View.GONE);
                }
                if (mlistener != null) {
                    mlistener.onPageSelected(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        pagerSlidingTabStrip.setViewPager(viewPager);
        pagerSlidingTabStrip.setOnPageChangeListener(mPageChangeListener);
        idStick.setStickNavAndScrollToNav();


        imgGotop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRecycleview.smoothScrollToPosition(0);
                int currentItem = viewPager.getCurrentItem();
                if (currentItem == 0) {
                    comListFragment.setGoTop();
                } else if (currentItem == 1) {
                    giftListFragment.setGoTop();
                }
            }
        });

        idStick.setOnFlingChangedListener(new OnFlingChangedListener() {
            @Override
            public void onComFlChanged(boolean isDown) {
                if (isDown && isStick) {
                    int itemCount = comListFragment.mAdapter.getListMatBean().size();
                    if (itemCount > 15)
                        imgGotop.setVisibility(View.VISIBLE);
                } else {
                    imgGotop.setVisibility(View.GONE);
                }
            }

            @Override
            public void onGifFlChanged(boolean isDown) {

            }
        });

//        mFragment = new MatOrGiftJPageFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString("HEADID", mHeadid);
//        bundle.putString("TYPE", mType);
//        mFragment.setArguments(bundle);
//        mActivity.getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, mFragment).commit();
    }


    private ViewPager.OnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            super.onPageScrollStateChanged(state);
        }
    };

    @Override
    public void onStart() {
        super.onStart();
//        ViewPager viewPager = mFragment.getmViewPager();
//        ViewPageFragmentAdapter vPAdapter = mAdapter.getmViewPagerAdapter();
//        ComListFragment com = (ComListFragment) mAdapter.instantiateItem(viewPager, 0);
//        com.setOnAllMoneyChanged(new ComListFragment.OnAllMoneyChanged() {
//            @Override
//            public void moneyChanged(double allmoney) {
//                tvAllMoney.setText(allmoney + "");
//            }
//        });
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_add_mat, menu);
        if ("show".equals(mType)) {
            MenuItem item = menu.getItem(0);
            item.setVisible(false);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.send_btn:
                subMat();
                break;
            case R.id.btn_save:
                saveMat();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void subMat() {
//        ViewPager viewPager = mFragment.getmViewPager();
//        ViewPageFragmentAdapter vPAdapter = mFragment.getmViewPagerAdapter();
        ComListFragment com = (ComListFragment) mAdapter.instantiateItem(viewPager, 0);
        GiftListFragment gift = (GiftListFragment) mAdapter.instantiateItem(viewPager, 1);
        List<MatBeanCount> coms = com.mAdapter.getListMatBean();
        List<MatBeanCount> gifts = gift.mAdapter.getListMatBean();
        if (coms == null || coms.size() == 0) {
            Snackbar.make(mActivity.getWindow().getDecorView(), "请扫描商品，再提交", Snackbar.LENGTH_SHORT).show();
            return;
        }
        String fhckId = head.getFhckId();
        String ghdwId = head.getGhdwId();
        String hybId = head.getHybId();
        String kfdhId = head.getKfdhId();
        String sklxId = head.getSklxId();
        String xsfsId = head.getXsfsId();
        String ywyId = head.getYwyId();
        String date = head.getDate();


        if (!StringUtils.isEmpty(fhckId) && !StringUtils.isEmpty(ghdwId) && !StringUtils.isEmpty(kfdhId) && !StringUtils.isEmpty(sklxId) &&
                !StringUtils.isEmpty(xsfsId) && !StringUtils.isEmpty(ywyId)) {

            if (!StringUtils.isToday(date)) {
                Snackbar.make(mActivity.getWindow().getDecorView(), "请修改提交日期！", Snackbar.LENGTH_SHORT).show();
                return;
            }

            ArrayList<MatBeanCount> allMats = new ArrayList<>();
            allMats.addAll(coms);
            allMats.addAll(gifts);
            List<MatSubBean.TableBody> bodys = new ArrayList<>();

            for (int i = 0; i < allMats.size(); i++) {
                MatBeanCount count1 = allMats.get(i);
                MatSubBean.TableBody body = new MatSubBean.TableBody();
                String count = count1.getCount();
                double saleprice = count1.getSaleprice();
                double Amount = ArithUtil.round(Integer.parseInt(count) * saleprice, 0);
                String type = count1.getType();
                if ("0".equals(type)) {//0代表商品  1赠品
                    double scoreByItemId = ScoreEntryManager.getInstance(mActivity).getScoreByItemId(count1.getId(), Integer.parseInt(count));
                    body.setFUnitScore(scoreByItemId + "");
                    body.setFItemScore(String.valueOf((scoreByItemId * Integer.parseInt(count))));
                } else if ("1".equals(type)) {
                    String credit = count1.getCredit();
                    if (StringUtils.isEmpty(credit)) {
                        credit = "0";
                    }
                    body.setFUnitTradeScore(credit);
                    body.setFTradeScore((Integer.parseInt(credit) * Integer.parseInt(count)) + "");
                }

                body.setFUnitId(count1.getUnit_id());
                body.setFProductID(count1.getId());
                body.setFSaleAmount("" + Amount);
                body.setFSalePrice(count1.getSaleprice() + "");
                body.setFQty(count1.getCount());

                String value = count1.getTDCStockID();
                String cusvalue = count1.getFDCStockID();
                if (!StringUtils.isEmpty(value)) {
                    body.setFStockId(value);//发货仓库
                } else if (!StringUtils.isEmpty(value)) {
                    body.setFStockId(cusvalue);//发货仓库
                } else {
                    body.setFStockId("0");//发货仓库
                }

                bodys.add(body);
            }

            MatSubBean.TableHead tableHead = new MatSubBean.TableHead();

            tableHead.setFDate(date);
            tableHead.setFCusPhone(kfdhId);
            tableHead.setFDCStockID(fhckId);
//            tableHead.setFExplanation(etPaper);
            tableHead.setFEmpID(ywyId);
//            tableHead.setFRealReceive(etMoney);
            tableHead.setFSaleStyle(xsfsId);
            tableHead.setFStockDept(hybId);
            tableHead.setFSupplyID(ghdwId);
            tableHead.setFReceiveType(sklxId);
            tableHead.setFtype("1");
            tableHead.setFExplanation(tvBz.getText().toString().trim() + "");

            showWaitDialog();
            MatSubBean matSubBean = new MatSubBean();
            matSubBean.setBodyList(bodys);
            matSubBean.setHeadDetaili(tableHead);
            Gson gson = new Gson();
            String json = gson.toJson(matSubBean);
//            PLog.i("lj", json);
            String token = AppContext.getInstance().getProperty(AppConfig.PRIVATE_TOKEN);
            CM cm = new CM();
            cm.setToken(token);
            cm.setData(json);

            RetrofitSingleton.getApiService(mActivity).
                    StockBillSubmit(cm)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);


        } else {
            Snackbar.make(mActivity.getWindow().getDecorView(), "必填信息不能为空！", Snackbar.LENGTH_SHORT).show();
        }

    }

    private void saveMat() {
//        ViewPager viewPager = mFragment.getmViewPager();
//        ViewPageFragmentAdapter vPAdapter = mFragment.getmViewPagerAdapter();
        ComListFragment com = (ComListFragment) mAdapter.instantiateItem(viewPager, 0);
        GiftListFragment gift = (GiftListFragment) mAdapter.instantiateItem(viewPager, 1);
        List<MatBeanCount> coms = com.mAdapter.getListMatBean();
        List<MatBeanCount> gifts = gift.mAdapter.getListMatBean();
        if (coms == null || coms.size() == 0) {
            Snackbar.make(mActivity.getWindow().getDecorView(), "请扫描商品，再保存", Snackbar.LENGTH_SHORT).show();
            return;
        }

        String fhckId = head.getFhckId();
        String ghdwId = head.getGhdwId();
        String hybId = head.getHybId();
        String kfdhId = head.getKfdhId();
        String sklxId = head.getSklxId();
        String xsfsId = head.getXsfsId();
        String ywyId = head.getYwyId();
        String date = head.getDate();
        head.setDesc(tvBz.getText().toString().trim());


//        if (!StringUtils.isEmpty(fhckId) && !StringUtils.isEmpty(ghdwId) && !StringUtils.isEmpty(kfdhId) && !StringUtils.isEmpty(sklxId) &&
//                !StringUtils.isEmpty(xsfsId) && !StringUtils.isEmpty(ywyId)) {
        MatHeadManager2.getInstance(mActivity).inOrRe(head);
        MatHead lastHead = MatHeadManager2.getInstance(mActivity).getLastHead();
        ArrayList<MatBeanCount> TmatBeans = new ArrayList<>();
        TmatBeans.addAll(coms);
        TmatBeans.addAll(gifts);
        for (int i = 0; i < TmatBeans.size(); i++) {
            TmatBeans.get(i).setHead(lastHead);
        }
        MatCountManager.getInstance(mActivity).insertMatList(TmatBeans);
        Snackbar.make(mActivity.getWindow().getDecorView(), "保存成功!", Snackbar.LENGTH_SHORT).show();
        isSave = true;
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mActivity.finish();
                Bundle bundle = new Bundle();
                UIHelper.showSimpleBack(mActivity, SimpleBackPage.ADD_SALE_OUT, bundle, "销售出库");
            }
        }, 1000);//3秒后执行Runnable中的run方法
//        } else {
//            Snackbar.make(mActivity.getWindow().getDecorView(), "必填信息不能为空！", Snackbar.LENGTH_SHORT).show();
//        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.ll_pgno:
                bundle.putString("type", "hyb");
                UIHelper.showSimpleBackForResult(this, mActivity, 6, SimpleBackPage.CUST_PHEON, bundle, "货运部");
                break;
            case R.id.ll_gh_unit:
                bundle.putString("type", "ghdw");
                UIHelper.showSimpleBackForResult(this, mActivity, 1, SimpleBackPage.CUST_PHEON, bundle, "购货单位");
                break;
            case R.id.ll_sales_way:
                bundle.putString("type", "xsfs");
                UIHelper.showSimpleBackForResult(this, mActivity, 2, SimpleBackPage.CUST_PHEON, bundle, "销售方式");
                break;
            case R.id.ll_pgno_type:
                bundle.putString("type", "sklx");
                UIHelper.showSimpleBackForResult(this, mActivity, 3, SimpleBackPage.CUST_PHEON, bundle, "收款类型");
                break;
            case R.id.ll_cust_phone:
                bundle.putString("type", "khdh");
                UIHelper.showSimpleBackForResult(this, mActivity, 0, SimpleBackPage.CUST_PHEON, bundle, "客服电话");
                break;
            case R.id.ll_sale:
                bundle.putString("type", "ywy");
                UIHelper.showSimpleBackForResult(this, mActivity, 4, SimpleBackPage.CUST_PHEON, bundle, "业务员");
                break;
            case R.id.ll_fh_warehouse:
                bundle.putString("type", "fhck");
                UIHelper.showSimpleBackForResult(this, mActivity, 5, SimpleBackPage.CUST_PHEON, bundle, "发货仓库");
                break;
            case R.id.ll_date:
                new SlideDateTimePicker.Builder(getFragmentManager())
                        .setListener(listener)
                        .setInitialDate(new Date())
                        .build()
                        .show();
                break;
        }
    }

    private SlideDateTimeListener listener = new SlideDateTimeListener() {

        @Override
        public void onDateTimeSet(Date date) {
            String str = StringUtils.getDateString(date);
            tvDate.setText(str);
            head.setDate(str);
        }

        @Override
        public void onDateTimeCancel() {
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            final CustPhone item = (CustPhone) data.getSerializableExtra("item");
            if (item != null) {
                String name = item.getName();
                String id = item.getId();
                switch (requestCode) {
                    case 0:
                        tvCustPhone.setText(name);
                        head.setKfdhName(name);
                        head.setKfdhId(id);
//                        MatHeadManager2.getInstance(mActivity).inOrRe(HeadBean);
                        break;
                    case 1:
                        tvGhUnit.setText(name);
                        head.setGhdwName(name);
                        head.setGhdwId(id);
//                        MatHeadManager2.getInstance(mActivity).inOrRe(HeadBean);
                        break;
                    case 2:
                        tvSalesWay.setText(name);
                        head.setXsfsName(name);
                        head.setXsfsId(id);
//                        MatHeadManager2.getInstance(mActivity).inOrRe(HeadBean);
                        break;
                    case 3:
                        tvPgnoType.setText(name);
                        head.setSklxName(name);
                        head.setSklxId(id);
//                        MatHeadManager2.getInstance(mActivity).inOrRe(HeadBean);
                        break;
                    case 4:
                        tvSale.setText(name);
                        head.setYwyName(name);
                        head.setYwyId(id);
//                        MatHeadManager2.getInstance(mActivity).inOrRe(HeadBean);
                        break;
                    case 5:
                        tvFhWarehouse.setText(name);
                        head.setFhckName(name);
                        head.setFhckId(id);

//                        ViewPager viewPager = mFragment.getmViewPager();
//                        ViewPageFragmentAdapter vPAdapter = mFragment.getmViewPagerAdapter();
                        ComListFragment com = (ComListFragment) mAdapter.instantiateItem(viewPager, 0);
                        GiftListFragment gift = (GiftListFragment) mAdapter.instantiateItem(viewPager, 1);
                        com.FDCStockId = id;
                        com.FDCStockName = name;
                        gift.FDCStockId = id;
                        gift.FDCStockName = name;
//                        List<MatBeanCount> coms = com.mAdapter.getListMatBean();
//                        List<MatBeanCount> gifts = gift.mAdapter.getListMatBean();
//                        ArrayList<MatBeanCount> TmatBeans = new ArrayList<>();
//                        TmatBeans.addAll(coms);
//                        TmatBeans.addAll(gifts);
//                        for (int i = 0; i < TmatBeans.size(); i++) {
//                            TmatBeans.get(i).setFDCStockName(name);
//                            TmatBeans.get(i).setFDCStockID(id);
//                        }
//                        com.mAdapter.notifyDataSetChanged();
//                        gift.mAdapter.notifyDataSetChanged();

                        break;
                    case 6:
                        tvPgno.setText(name);
                        head.setHybName(name);
                        head.setHybId(id);
//                        MatHeadManager2.getInstance(mActivity).inOrRe(HeadBean);
                        break;
                    case 7://adapter中点击发货仓库 自选的仓库
//                        ArrayList<BackMatBean> backList = mAdapter.getBackList();
//                        BackMatBean backMatBean = backList.get(position);
//                        backMatBean.setTDCStockID(item.getId());
//                        backMatBean.setTDCStockName(item.getName());
//                        backList.set(position, backMatBean);
//                        mAdapter.setBackList(backList);
//                        mAdapter.notifyItemChanged(position);
                        break;

                }
            }
        }
    }

    Observer<Result> observer = new Observer<Result>() {
        @Override
        public void onCompleted() {
            PLog.d("s", "onCompleted");
            hideWaitDialog();
        }

        @Override
        public void onError(Throwable e) {
            PLog.d("s", e.getMessage());
            String errBody = ErrorHandler.handle(e);
            Snackbar.make(getActivity().getWindow().getDecorView(), errBody, Snackbar.LENGTH_SHORT).show();
            hideWaitDialog();
        }

        @Override
        public void onNext(Result result) {
            hideWaitDialog();
            if (result.isSuccess()) {
                Snackbar.make(mActivity.getWindow().getDecorView(), "提交成功!", Snackbar.LENGTH_SHORT).show();
                if ("show".equals(mType) || isSave) {
                    MatHeadManager2.getInstance(mActivity).delAllById(head.getId());
                    MatCountManager.getInstance(mActivity).delAllMatByHeadId(head.getId());//提交成功后删除缓存
                }
                mActivity.finish();
            } else {
                Snackbar.make(mActivity.getWindow().getDecorView(), result.getMessage(), Snackbar.LENGTH_SHORT).show();
            }
        }
    };
}
